/*
 * ============================================================================
 *
 *  [THC RPG] Total HardCore RPG
 *
 *  File:          xpsystem.inc
 *  Type:          Core
 *  Description:   contains functions specific to the XP system (xp, level, credits)
 *
 *  Copyright (C) 2009-2011  ArsiRC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ============================================================================
 */

#pragma semicolon 1

SetXP(client,amount,display)
{
    if(client)
    {
        new isbot=IsFakeClient(client);
        if(g_CoreConfigCache[CoreConfig_bot_enable]||!g_CoreConfigCache[CoreConfig_bot_enable]&&!isbot)
        {
            if(g_CoreConfigCache[CoreConfig_bot_control]&&isbot)
            {
                new count=CountHumans();
                if(!count)
                    return -1;
            }
            new reqxp=GetReqXP(client);
            new xp=GetXP(client);
            new newxp=xp+amount;
            new level=0;
            while(newxp>reqxp)
            {
                level+=1;
                reqxp+=g_CoreConfigCache[CoreConfig_exp_inc];
            }
            while(newxp<reqxp-g_CoreConfigCache[CoreConfig_exp_inc])
            {
                level-=1;
                reqxp-=g_CoreConfigCache[CoreConfig_exp_inc];
            }
            if(level!=0)
                SetLevel(client,level,display);
            new vecPosi = GetPlayerVectorPosition(client);
            if(vecPosi!=-1)
                SetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_XP,newxp);
            else
                return -1;
            if(display)
            {
                reqxp=GetReqXP(client);
                if(amount>0)
                    TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Chat, INVALID_MODULE, false, "Give XP", amount, newxp, reqxp);
                else if(amount<0)
                    TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Chat, INVALID_MODULE, false, "Take XP", amount, newxp, reqxp);
            }
            return 1;
        }
        else
            return 0;
    }
    else
        return -1;
}

SetStaticXP(client,amount)
{
    if(client)
    {
        if(amount<0)
            amount=0;
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
        {
            SetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_XP,amount);
            return 1;
        }
        else
            return -1;
    }
    else
        return -1;
}

GetXP(client)
{
    if(client)
    {
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
        {
            return GetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_XP);
        }
        else
            return -1;
    }
    else
        return -1;
}

SetReqXP(client,amount)
{
    if(client)
    {
        new isbot=IsFakeClient(client);
        if(g_CoreConfigCache[CoreConfig_bot_enable]||!g_CoreConfigCache[CoreConfig_bot_enable]&&!isbot)
        {
            if(g_CoreConfigCache[CoreConfig_bot_control]&&isbot)
            {
                new count=CountHumans();
                if(!count)
                    return -1;
            }
            new newamount=GetReqXP(client)+amount;
            if(newamount<g_CoreConfigCache[CoreConfig_exp_start])
                newamount=g_CoreConfigCache[CoreConfig_exp_start];
            if(newamount>g_CoreConfigCache[CoreConfig_exp_max])
                newamount=g_CoreConfigCache[CoreConfig_exp_max];
            new vecPosi = GetPlayerVectorPosition(client);
            if(vecPosi!=-1)
            {
                SetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_REQXP,newamount);
                return 1;
            }
            else
                return -1;
        }
        else
            return 0;
    }
    else
        return -1;
}

SetStaticReqXP(client,amount)
{
    if(client)
    {
        if(amount<g_CoreConfigCache[CoreConfig_exp_start])
            amount=g_CoreConfigCache[CoreConfig_exp_start];
        if(amount>g_CoreConfigCache[CoreConfig_exp_max])
            amount=g_CoreConfigCache[CoreConfig_exp_max];
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
        {
            SetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_REQXP,amount);
            return 1;
        }
        else
            return -1;
    }
    else
        return -1;
}

GetReqXP(client)
{
    if(client)
    {
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
            return GetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_REQXP);
        else
            return -1;
    }
    else
        return -1;
}

SetLevel(client,amount,display)
{
    if(client)
    {
        new isbot=IsFakeClient(client);
        if(g_CoreConfigCache[CoreConfig_bot_enable]||!g_CoreConfigCache[CoreConfig_bot_enable]&&!isbot)
        {
            if(g_CoreConfigCache[CoreConfig_bot_control]&&isbot)
            {
                new count=CountHumans();
                if(!count)
                    return -1;
            }
            new level=GetLevel(client);
            new newlevel=level+amount;
            if(newlevel<1)
                newlevel=1;
            new vecPosi = GetPlayerVectorPosition(client);
            if(vecPosi!=-1)
                SetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_LEVEL,newlevel);
            else
                return -1;
            if(amount>0&&isbot)
                BotBuyRandomUpgrade(client);
            if(amount!=0)
            {
                SetCredits(client,g_CoreConfigCache[CoreConfig_credits_inc]*amount);
                SetReqXP(client,g_CoreConfigCache[CoreConfig_exp_inc]*amount);
                SavePlayerData(client); // Save player's data
                decl String:name[64];
                GetClientName(client,name,sizeof(name));
                if(display)
                {
                    for(new x=1;x<=MaxClients;x++)
                        if(IsClientInGame(x))
                            TransMgr_PrintText(x, MsgFormat_Plugin, MsgType_Chat, INVALID_MODULE, false, "Announce levelup", name, amount);
                    TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Chat, INVALID_MODULE, false, "Notify levelup", GetCredits(client));
                    PlayLevelupSound(client);
                }
                // Check if bot went over maxlevel
                if(isbot&&amount>0&&amount>=g_CoreConfigCache[CoreConfig_bot_maxlevel])
                {
                    ResetPlayer(client);
                    for(new x=1;x<=MaxClients;x++)
                        if(IsClientInGame(x))
                            TransMgr_PrintText(x, MsgFormat_Plugin, MsgType_Chat, INVALID_MODULE, false, "Bot reset stats", name, g_CoreConfigCache[CoreConfig_bot_maxlevel]);
                }
                return 1;
            }
            return 1;
        }
        else
            return 0;
    }
    else
        return -1;
}

SetStaticLevel(client,amount)
{
    if(client)
    {
        if(amount<0)
            amount=0;
        new vecPosi=GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
        {
            SetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_LEVEL,amount);
            return 1;
        }
        else
            return -1;
    }
    else
        return -1;
}

GetLevel(client)
{
    if(client)
    {
        new vecPosi=GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
            return GetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_LEVEL);
        else
            return -1;
    }
    else
        return -1;
}

SetCredits(client,amount)
{
    if(client)
    {
        new isbot=IsFakeClient(client);
        if(g_CoreConfigCache[CoreConfig_bot_enable]||!g_CoreConfigCache[CoreConfig_bot_enable]&&!isbot)
        {
            if(g_CoreConfigCache[CoreConfig_bot_control]&&isbot)
            {
                new count=CountHumans();
                if(!count)
                    return -1;
            }
            new newcredits=GetCredits(client)+amount;
            if(newcredits<0)
                newcredits=0;
            new vecPosi = GetPlayerVectorPosition(client);
            if(vecPosi!=-1)
            {
                SetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_CREDITS,newcredits);
                return 1;
            }
            else
                return -1;
        }
        else
            return 0;
    }
    else
        return -1;
}

SetStaticCredits(client,amount)
{
    if(client)
    {
        if(amount<0)
            amount=0;
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
        {
            SetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_CREDITS,amount);
            return 1;
        }
        else
            return -1;
    }
    else
        return -1;
}

GetCredits(client)
{
    if(client)
    {
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
            return GetArrayCell(GetArrayCell(vecPlayers,vecPosi),VECTOR_PLAYERS_CREDITS);
        else
            return -1;
    }
    else
        return -1;
}
